package cn.marinda.gui.impl;

import cn.marinda.entity.AbstractServerCore;
import cn.marinda.entity.AbstractServerPlugin;
import cn.marinda.entity.ComponentSource;
import cn.marinda.gui.AbstractHelper;
import cn.marinda.listener.*;
import lombok.Data;

import javax.swing.*;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * 创建服务端界面
 *
 * @author marinda
 * @date 2022/07/13
 */
@Data
public class CreateServerHelperImpl extends AbstractHelper {

    public static CreateServerHelperImpl instance = new CreateServerHelperImpl();
//    列表源

    /**
     * 创建服务端界面
     */
    private CreateServerHelperImpl(){
        super();
    }

    @Override
    protected JFrame loadFrame() {
        frame = new JFrame();
        frame.setSize(700,930);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setTitle("创建服务端");
        frame.setJMenuBar(menuBar);
        frame.setContentPane(view);
        frame.pack();
        return frame;
    }

    /**
     * 获取基本组件
     *
     * @return boolean
     */
    private void saveBasicComponent(Box panelBox){
        List<Box> boxList = new ArrayList<>();
        for(String key : componentMap.keySet()){
            //        直接遍历所有内容
            List<ComponentSource> source = componentMap.get(key);
            for(ComponentSource componentSource : source){
                Box box1 = Box.createHorizontalBox();
                if(componentSource.getComponent() == null){
                    box1.add(Box.createHorizontalStrut(50));
                    box1.add(componentSource.getLabel());
                    box1.add(Box.createHorizontalStrut(10));
                    box1.add(componentSource.getComponentList().get(0));
                    box1.add(componentSource.getComponentList().get(1));
                    box1.add(Box.createHorizontalStrut(100));
                    box1.add(Box.createVerticalStrut(30));
                }else{
                    box1.add(Box.createHorizontalStrut(50));
                    box1.add(componentSource.getLabel());
                    box1.add(Box.createHorizontalStrut(10));
                    box1.add(componentSource.getComponent());
                    box1.add(componentSource.getComponent());
                    box1.add(Box.createHorizontalStrut(100));
                    box1.add(Box.createVerticalStrut(30));
                }
                boxList.add(box1);
            }
        }
        for(Box boxs : boxList){
            panelBox.add(boxs);
            panelBox.add(Box.createVerticalStrut(30));
        }
        JList pluginList = (JList) getHelperComponent("ListUI");
        pluginList.addListSelectionListener(new PluginListSelectionEvent(pluginList));
        JComboBox<String> serverCore = (JComboBox<String>) getHelperComponent("ComboBoxUI");
        JButton btn = createButtonComponent("创建服务端",Color.red);
        btn.addActionListener(new CreateServerEvent(btn,serverCore,createRadioMap,componentMap));
        panelBox.add(btn);
    }


    @Override
    protected JPanel loadPanel() {
        view = new JPanel();
        Box boxLineFirst = Box.createHorizontalBox();
        boxLineFirst.add(Box.createHorizontalStrut(200));
        JLabel label = getHelperComponentLabelByText("服务端创建");
        boxLineFirst.add(label);
        boxLineFirst.add(Box.createHorizontalGlue());

        boxPanel.add(boxLineFirst);
        boxPanel.add(Box.createVerticalStrut(20));
        saveBasicComponent(boxPanel);
        boxPanel.add(Box.createHorizontalGlue());
        view.add(boxPanel);
        return view;
    }


    @Override
    protected List<Component> loadComponent() {
//      基础配置
        JLabel title = createTextComponent("服务端创建");
        JLabel coreLabel = createTextComponent("核心：");
        JLabel pluginLabel = createTextComponent("插件：");
        JLabel settingLabel = createTextComponent("基本设置");
        JLabel eulaLabel = createTextComponent("eula: ");
//      服务端配置
        JLabel serverSettingLabel = createTextComponent("服务端配置");
        JLabel onlineLabel = createTextComponent("正版验证：");
        JLabel maxPlayerLabel = createTextComponent("最大玩家数：");
        JLabel motdLabel = createTextComponent("motd：");
        JLabel whiteLabel = createTextComponent("白名单：");
        JLabel portLabel = createTextComponent("端口号：");
        JLabel ipLabel = createTextComponent("ip：");

//      基础配置相关控件
        JComboBox<String> serverCoreList = createComboBoxComponent(AbstractServerCore.coreList);
        serverCoreList.setSelectedIndex(0);
//        插件配置
        JList<String> serverPluginList = createListComponent(AbstractServerPlugin.pluginList);
//        mod配置
//        JList<String> serverModList = createListComponent(AbstractServerPlugin.pluginList);

//        eula协议的单选按钮组
        ButtonGroup eulaGroup = new ButtonGroup();
        JRadioButton eulaAgree = createRadioButtonComponent("同意");
        JRadioButton eulaRefuse = createRadioButtonComponent("拒绝");
        eulaGroup.add(eulaAgree);
        eulaGroup.add(eulaRefuse);
//        默认同意eula协议
        eulaAgree.setSelected(true);
        eulaRefuse.setEnabled(false);

//        正版验证
        ButtonGroup onlineFlag = new ButtonGroup();
        JRadioButton onlineAgree = createRadioButtonComponent("开启");
        JRadioButton onlineRefuse = createRadioButtonComponent("关闭");
        onlineRefuse.setSelected(true);
        onlineFlag.add(onlineAgree);
        onlineFlag.add(onlineRefuse);
//        最大玩家数
        JTextField maxOnlineTextField = createTextFieldComponent(5);
//        motd
        JTextField motdTextField = createTextFieldComponent(5);

        ButtonGroup whiteFlag = new ButtonGroup();
        JRadioButton whiteAgree = createRadioButtonComponent("开启");
        JRadioButton whiteRefuse = createRadioButtonComponent("关闭");
        whiteRefuse.setSelected(true);

        whiteFlag.add(whiteAgree);
        whiteFlag.add(whiteRefuse);

        //        最大玩家数
        JTextField portTextField = createTextFieldComponent(5);
        JTextField ipTextField = createTextFieldComponent(5);

//        创建服务端
        JButton createServerBtn = createButtonComponent("创建服务端",Color.GRAY);

        List<ComponentSource> componentSources = new ArrayList<>();
        componentSources.add(new ComponentSource(coreLabel,serverCoreList));
        componentSources.add(new ComponentSource(pluginLabel,serverPluginList));
//        componentSources.add(new ComponentSource(modLabel,serverModList));
        List<JComponent> eulaList = new ArrayList<>();
        eulaList.add(eulaAgree);
        eulaList.add(eulaRefuse);
        componentSources.add(new ComponentSource(eulaLabel,eulaList));
        componentMap.put("基础配置",componentSources);
        maxOnlineTextField.setText("2022");
        motdTextField.setText("A Minecraft Server!");
        List<ComponentSource> serverSettingSources = new ArrayList<>();
        List<JComponent> onlineRadioList = new ArrayList<>();
        onlineRadioList.add(onlineAgree);
        onlineRadioList.add(onlineRefuse);
        serverSettingSources.add(new ComponentSource(onlineLabel,onlineRadioList));

        serverSettingSources.add(new ComponentSource(createTextComponent("max-players"),maxOnlineTextField));
        serverSettingSources.add(new ComponentSource(createTextComponent("motd"),motdTextField));

        List<JComponent> whiteRadioList = new ArrayList<>();
        whiteRadioList.add(whiteAgree);
        whiteRadioList.add(whiteRefuse);
        serverSettingSources.add(new ComponentSource(whiteLabel,whiteRadioList));
        serverSettingSources.add(new ComponentSource(createTextComponent("server-port"),portTextField));
        serverSettingSources.add(new ComponentSource(createTextComponent("server-ip"),ipTextField));
        ipTextField.setText("127.0.0.1");
        portTextField.setText("25565");
        portTextField.setToolTipText("最多输入5个文本！");
        for(ComponentSource componentSource : serverSettingSources){
            if(componentSource.getComponent() instanceof JTextField){
                JTextField jtext = (JTextField) componentSource.getComponent();
            }
        }
        componentMap.put("服务端配置",serverSettingSources);
        //        统一储存

        componentList.add(title);
        componentList.add(coreLabel);
        componentList.add(pluginLabel);
        componentList.add(settingLabel);
        componentList.add(eulaLabel);
        componentList.add(serverSettingLabel);
        componentList.add(onlineLabel);
        componentList.add(maxPlayerLabel);
        componentList.add(motdLabel);
        componentList.add(whiteLabel);
        componentList.add(portLabel);
        componentList.add(ipLabel);
        componentList.add(serverCoreList);
        componentList.add(serverPluginList);
        componentList.add(eulaAgree);
        componentList.add(eulaRefuse);
        componentList.add(onlineAgree);
        componentList.add(onlineRefuse);
        componentList.add(maxOnlineTextField);
        componentList.add(motdTextField);
        componentList.add(whiteAgree);
        componentList.add(whiteRefuse);
        componentList.add(portTextField);
        componentList.add(ipTextField);
        componentList.add(createServerBtn);
//        单选按钮储存
        ArrayList<JRadioButton> onlineArrayList = new ArrayList();
        onlineArrayList.add(onlineAgree);
        onlineArrayList.add(onlineRefuse);
        ArrayList whiteArrayList = new ArrayList();
        whiteArrayList.add(whiteAgree);
        whiteArrayList.add(whiteRefuse);
        putRadioButtonValue("online-mode",onlineArrayList);
        putRadioButtonValue("white-list",whiteArrayList);
        return componentList;
    }

    /**
     * put单选按钮值
     *
     * @param key  关键
     * @param list 列表
     */
    private void putRadioButtonValue(String key,List<JRadioButton> list){
        createRadioMap.put(key,list);
    }

}